package command;

import java.io.IOException;

import org.apache.xmlrpc.XmlRpcException;

import xmlrpc.client.ServerCommunicator;
import core.Constants;

public class CommandBroadcaster {
	private static ServerCommunicator sc = new ServerCommunicator();
	/**
	 * Checks if connected and sends command via the ServerCommunicator if so.
	 * Else just executes command.
	 * @param c
	 */
	public static void sendCommand(Command c) {
		if(c != null){
			switch (Constants.connectionState) {
			case CONNECTED:
				String string2Send = c.toString();
				try {
					sc.sendCommandString(string2Send);
				} catch (XmlRpcException e) {
					System.out.println("Error when sending commandstring!");
					e.printStackTrace();
				}
				break;
	
			case DISCONNECTED:
				CommandExecutor.getInstance().executeCommand(c);
				break;
			}
		}

	}
	
	public static String getCommand(){
		return sc.getCommandString();
		
	}
	public static int getPlayerId(){
		return sc.getPlayerId();
	}
	public static String pingServer(){
		try {
			return sc.ping();
		} catch (XmlRpcException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "failed ping!";
		
	}
}
